/**
 * @file port_ble_base.h
 * @brief 
 * @author dotc (dotchan@qq.com)
 * @date 2025-01-21
 * 
 * @copyright Copyright (c) 2025 by 深圳市鹏天微智能科技有限公司, All Rights Reserved.
 */

#ifndef __PORT_BLE_BASE_H__
#define __PORT_BLE_BASE_H__

#if defined(CONFIG_PORT_BLE_ENABLE)

/* ==================== [Includes] ========================================== */

#include "xf_utils.h"
#include "gatt.h"
#include "gapm.h"
#include "gapc.h"

#ifdef __cplusplus
extern "C" {
#endif

/* ==================== [Defines] =========================================== */

#define DEFAULT_BLE_APP_MAX_CNT     (1)
#define PORT_BLE_INVALID_LINK_ID      (UINT8_MAX)
#define PORT_BLE_UUID_TYPE_GET_FROM_XF(xf_uuiid_type)     (((xf_uuiid_type)+4)>>3)
#define PORT_GATT_APP_LIST_INDEX_TO_ID(list_index)      ((list_index)+1)
#define PORT_GATT_APP_ID_TO_LIST_INDEX(app_id)          ((app_id)-1)

/* ==================== [Typedefs] ========================================== */

typedef struct _port_ble_gatt_info_t
{
    uint16_t   start_hdl;
} port_ble_gatt_info_t;

typedef struct _port_ble_gatt_att_value_t
{
    uint16_t    hdl;
    uint8_t     *value;
    uint16_t    value_len;
} port_ble_gatt_att_value_t;

typedef struct _port_ble_gatt_app_t
{
    bool        is_inited;
    uint8_t     conn_id;
    uint8_t     app_id;
    uint16_t    start_hdl;
    uint16_t    att_cnt;
    port_ble_gatt_att_value_t *att_val_set;
} port_ble_gatt_app_t;

typedef struct _port_ble_gatt_client_t
{
    bool        is_inited;
    uint16_t    start_hdl;
    uint16_t    att_cnt;
} port_ble_gatt_client_t;

typedef struct _port_ble_gatt_server_t
{
    bool        is_inited;
    uint16_t    start_hdl;
    uint16_t    att_cnt;
    port_ble_gatt_att_value_t *att_val_set;
} port_ble_gatt_server_t;

typedef struct _port_ble_app_t
{
    uint8_t role;
    uint8_t conn_id;
    uint8_t app_id;
    union 
    {
        void *_p_all;
        port_ble_gatt_server_t *p_server;
        port_ble_gatt_client_t *p_client;
    };
} port_ble_app_t;

/* ==================== [Global Prototypes] ================================= */

uint8_t port_gatt_app_id_alloc(void);
port_ble_app_t *port_gatt_app_list_get(void);
port_ble_app_t *port_gatt_app_get(uint8_t app_id);
port_ble_app_t *port_gatt_app_get_by_conn_id(uint8_t conn_id);
uint16_t port_gatt_handle_alloc(void);
uint16_t port_gatt_start_handle_get(void);
void port_gatt_start_handle_set(uint16_t handle);

/* ==================== [Macros] ============================================ */

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif

#endif // __PORT_BLE_BASE_H__
